/**
 * 
 */
package com.celephais.common.client.ui;

import com.celephais.common.client.model.TransferObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Eugene Shen
 *
 * This is an ugly class, but it gets the job done for now.
 * Basically, all it does is wrap an ObjectAccessTable into a dialog box.
 */
public class ChildCollectionEditorDialog extends DialogBox {
	
	private ModalDialogCallback owner;
	// Parameterizing with TransferObject is KIND OF a cop out, but
	// it should work...
	private ChildCollectionEditorCallback<TransferObject> tableCallback;
	private VerticalPanel topPanel;
	private ObjectAccessTable oat;
	private Button closeBtn;
	
	public ChildCollectionEditorDialog(ModalDialogCallback owner) {
		super(false, true);
		this.owner = owner;
		initComps();
	}

	private void initComps() {
		tableCallback = new ChildCollectionEditorCallback<TransferObject>();
		topPanel = new VerticalPanel();
		oat = new ObjectAccessTable(tableCallback, tableCallback);
		closeBtn = new Button("Close");
		
		closeBtn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ChildCollectionEditorDialog.this.hide();
				owner.onDialogConfirm(ChildCollectionEditorDialog.this);
			}
		});
		
		topPanel.add(oat);
		topPanel.add(closeBtn);
		add(topPanel);
	}
	
	@Override
	public void center() {
		super.center();
		oat.updateTable(true); // Make sure the columns are forcibly updated.
	}
	
	public void center(TransferObject parentObj, String colName) {
		setParentObj(parentObj);
		setColName(colName);
		center();
	}
	
	public void setParentObj(TransferObject parentObj) {
		tableCallback.setParentObj(parentObj);
	}
	public TransferObject getParentObj() {
		return tableCallback.getParentObj();
	}

	public void setColName(String colName) {
		tableCallback.setChildColName(colName);
	}
	public String getColName() {
		return tableCallback.getChildColName();
	}
}
